package com.pug.service;

import com.pug.domain.Order;
import com.pug.feign.UserServiceFeign;
import com.pug.mapper.OrderMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022-07-06$ 16:24$
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderService {

    public final OrderMapper orderMapper;
    public final UserServiceFeign userServiceFeign;

    /**
     * 下单
     *
     * @param userId 用户id
     * @param productId 产品id
     * @param count 数量
     * @return
     */
    public boolean create(Long userId, Long productId, Integer count) {
        // 下单的金额  从redis获取 - 经常修改的数据
        String price = count * 100 + "";
        // 开始生成订单
        Order order = new Order();
        order.setOrdernumber(UUID.randomUUID().toString());
        order.setCount(count);
        order.setProductId(productId);
        order.setUserId(userId);
        order.setPrice(price);
        // 新增订单
        int insert = orderMapper.insert(order);
        if (insert > 0) {
            // 远程调用用户服务 扣减用户的余额
            return userServiceFeign.discountbit(userId, price);
        }
        return false;
    }
}
